<?php
/**
 * Created by PhpStorm.
 * User: hui
 * Date: 2017/2/18
 * Time: 17:46
 */
namespace app\api\controller;

class Douyu extends Base
{
    protected $type = [
        'csgo' => 'csgo',
        'dota2' => 'dota2',
    ];

    public function select($type)
    {
        $type = $this->type[strtolower($type)];
        $website = $this->getWeb('douyu');
        $url = $website['url'].$type.'?limit=100';
        $data = $this->getData($url);
        if ($data['error'] === 0) {
            $liveInfo = [];
            foreach ($data['data'] as $roomInfo) {
                if (strlen($roomInfo['online']) >4) {
                    $roomInfo['views'] = round(($roomInfo['online'] / 10000),1) . '万';
                }else {
                    $roomInfo['views'] = $roomInfo['online'];
                }
                $liveInfo[] = [
                    'room_id'         => $roomInfo['room_id'],
                    'room_name'       => $roomInfo['room_name'],
                    'room_views'      => $roomInfo['views'],
                    'room_views_real' => $roomInfo['online'],
                    'room_username'   => $roomInfo['nickname'],
                    'room_image_url'  => $roomInfo['room_src'],
                    'room_url'        => 'https://www.douyu.com/' . $roomInfo['room_id'],
                    'room_offer'      => 'douyu'
                ];
            }
            return $liveInfo;
        } else {
            return false;
        }
    }
}